<template>
  <div class="app-container">
     <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
       <el-form-item label="建设项目" prop="projectId">
         <el-select
           v-model="queryParams.projectId"
           placeholder="请选择"
           clearable
           size="small"
           style="width: 240px"
           @change="handleQuery"
         >
           <el-option
             v-for="item in projectOptions"
             :key="item.projectId"
             :value="item.projectId"
             :label="item.projectName"></el-option>
         </el-select>
       </el-form-item>
       <el-form-item label="报表类型" prop="reportType">
         <el-select v-model="queryParams.reportType" placeholder="请选择报表类型" clearable size="small" @change="handleQuery">
           <el-option
             v-for="dict in dict.type.sys_report_type"
             :key="dict.value"
             :label="dict.label"
             :value="dict.value"
           />
         </el-select>
       </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openSelectReport"
          v-hasPermi="['system:report:add']"
        >添加报表</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="cancelAuthRoleReportAll"
          v-hasPermi="['system:report:remove']"
        >批量取消授权</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目名称" prop="projectName" align="left" :show-overflow-tooltip="true" />
      <el-table-column label="报表名称" prop="reportName" align="left" :show-overflow-tooltip="true" />
      <el-table-column label="报表类型" align="center" prop="reportType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_report_type" :value="scope.row.reportType"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="cancelAuthRoleReport(scope.row)"
            v-hasPermi="['system:report:remove']"
          >取消授权</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <select-report ref="select" :roleId="queryParams.roleId" @ok="handleQuery" />
  </div>
</template>

<script>
import { allocatedRoleReportList, authRoleReportCancel, authRoleReportCancelAll } from "@/api/project/report";
import selectReport from "./selectReport";
import {selectProjectList} from "@/api/project/project";

export default {
  name: "AuthRoleReport",
  dicts: ['sys_report_type'],
  components: { selectReport },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中用户组
      reportIds: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报表表格数据
      reportList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleId: undefined,
        reportType: undefined
      },
      // 建设项目选项
      projectOptions: [],
    };
  },
  created() {
    const roleId = this.$route.params && this.$route.params.roleId;
    if (roleId) {
      this.queryParams.roleId = roleId;
      this.getList();
      this.getProjectList();
    }
  },
  methods: {
    /** 建设项目下拉 */
    getProjectList() {
      selectProjectList().then().then(response => {
        this.projectOptions = response.projects;
      })
    },
    /** 查询授权用户列表 */
    getList() {
      this.loading = true;
      allocatedRoleReportList(this.queryParams).then(response => {
          this.reportList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 返回按钮
    handleClose() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({ path: "/system/role" });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.reportIds = selection.map(item => item.reportId)
      this.multiple = !selection.length
    },
    /** 打开授权用户表弹窗 */
    openSelectReport() {
      this.$refs.select.show();
    },
    /** 取消授权按钮操作 */
    cancelAuthRoleReport(row) {
      const roleId = this.queryParams.roleId;
      this.$modal.confirm('确认要取消该报表授权吗？').then(function() {
        return authRoleReportCancel({ reportId: row.reportId, roleId: roleId });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消授权成功");
      }).catch(() => {});
    },
    /** 批量取消授权按钮操作 */
    cancelAuthRoleReportAll(row) {
      const roleId = this.queryParams.roleId;
      const reportIds = this.reportIds.join(",");
      this.$modal.confirm('是否取消选中报表授权数据项？').then(function() {
        return authRoleReportCancelAll({ roleId: roleId, reportIds: reportIds });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消授权成功");
      }).catch(() => {});
    }
  }
};
</script>
